package com.zhang.service.Impl;

import com.zhang.domain.Menu;
import com.zhang.domain.MenuList;

import com.zhang.mapper.MenuMapper;
import com.zhang.mapper.PermissionMapper;
import com.zhang.service.ILoginInfoService;
import com.zhang.domain.LoginDto;
import com.zhang.mapper.LogininfoMapper;
import com.zhang.util.AjaxResult;
import com.zhang.util.MD5Utils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author zh
 * @create 2022-03-03 16:48
 */
@Service
public class LoginInfoServiceImpl implements ILoginInfoService {
    @Resource
    private LogininfoMapper mapper;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private MenuMapper menuMapper;
    @Resource
    private PermissionMapper permissionMapper;
    @Override
    public AjaxResult login(LoginDto loginDto) {
        LoginDto user = mapper.queryByUserName(loginDto);
        if (user != null) {
            if (loginDto.getType().equals(user.getType())) {
                String pwd = MD5Utils.encrypByMd5(loginDto.getPassword() + "zhang" + user.getSalt() + "hou");
                if (pwd.equals(user.getPassword())) {
                    UUID uuid = UUID.randomUUID();
                    redisTemplate.opsForValue().set(uuid.toString(), loginDto.getUsername(), 30, TimeUnit.MINUTES);
                    user.setUsername(loginDto.getUsername());
                    user.setToken(uuid.toString());
                    ArrayList<MenuList> list = new ArrayList<>();
                    if(loginDto.getType()==0){
                        List<Menu> menus = menuMapper.selectMenu(user.getId());
                        for (Menu menu : menus) {
                            if(menu.getIndex()==1){
                                MenuList menuList1 = new MenuList();
                                menuList1.setId(menu.getId());
                                menuList1.setName(menu.getName());
                                menuList1.setPath(menu.getPath());
                                list.add(menuList1);
                            }
                        }
                        for (MenuList menuList : list) {
                            ArrayList<MenuList> list2 = new ArrayList<>();
                            for (Menu menu : menus) {
                                if(menu.getIndex()==2&&menu.getParent_id().equals(menuList.getId())){
                                    MenuList menuList1 = new MenuList();
                                    menuList1.setName(menu.getName());
                                    menuList1.setPath(menu.getPath());
                                    list2.add(menuList1);
                                }
                            }
                            if(list2.size()>0){
                                menuList.setChildren(list2);
                            }
                        }
                    }
                    user.setMenuList(list);
//                    ArrayList<Map<String,String>> list = new ArrayList<>();
//                    HashMap<String, String> map = new HashMap<>();
//                    map.put("path","/admin");
//                    map.put("name","Admin");
//                    list.add(map);
//                    map=  new HashMap<>();
//                    map.put("path","/dashboard");
//                    map.put("name","Dashboard");
//                    list.add(map);
//                    map=  new HashMap<>();
//                    map.put("path","/admin/dept");
//                    map.put("name","Dept");
//                    list.add(map);
//                    user.setMenuList(list);
                    return AjaxResult.me(user);
                }else {
                    return AjaxResult.me(false, "用户名或密码错误");
                }
            }else {
                return AjaxResult.me(false, "用户名或密码错误");
            }
        }else {
            return AjaxResult.me(false, "用户名或密码错误");
        }
    }
}
